package com.jfsnpm.jfsnpm.core.web.controller;

import java.io.File;

import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;
import com.jfsnpm.jfsnpm.core.dao.User;
import com.jfsnpm.jfsnpm.core.service.UserService;
import com.jfsnpm.jfsnpm.core.util.AppHelper;
import com.jfsnpm.jfsnpm.plugin.bjui.BjuiRender;
import com.jfsnpm.jfsnpm.plugin.bjui.JfsnpmFileRender;

public class FileController extends Controller {
	public void upload(){
		User userinfor = UserService.getUserInfor(this);
		UploadFile uploadFile = getFile();
		File file = uploadFile.getFile();
		String fileType = getPara("type","public");
		String releKey = getPara("rele","");
		String fileName = getPara("fileName","none");
		String fileId = AppHelper.getUUID();
		String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
		File filePath = new File(uploadFile.getSaveDirectory()+fileType+File.separator);
		if(!filePath.exists() && !filePath.isDirectory()){
			filePath.mkdir();
		}
		//移动位置
		File fileNew = new File(uploadFile.getSaveDirectory()+fileType+File.separator+fileId+"."+fileExt);
		file.renameTo(fileNew);
		
		Record record = new Record().set("id", fileId)
				.set("fileName", fileName).set("fileType", fileType)
				.set("releKey", releKey).set("modifier", userinfor.getUserId())
				.set("modifyDate", AppHelper.getNow())
				.set("filePath", fileNew.getPath());
		if(Db.save("jfsnpm_file", record)){
			render(BjuiRender.success("上传成功", false));
		}else{
			render(BjuiRender.error("上传失败"));
		}
		
	}
	public void download(){
		String id = getPara("id");
		if(AppHelper.isEmpty(id)){
			renderHtml("{\"statusCode\":\"300\",\"message\":\"参数有误\"}");
			return;
		}
		Record record = Db.findById("jfsnpm_file", id);
		String filePath = record.getStr("filePath");
		String fileName = record.getStr("fileName");
		if(AppHelper.isEmpty(filePath)){
			renderHtml("{\"statusCode\":\"300\",\"message\":\"文件地址有误\"}");
			return;
		}
		File file = new File(filePath);
		if (file == null || !file.exists()||file.isDirectory()){
			renderHtml("{\"statusCode\":\"300\",\"message\":\"文件没有找到\"}");
			return;
		}
		render(new JfsnpmFileRender(file, fileName));
	}
	//简单文件查看删除
	public void list(){
		String path=getPara("path","");
		String basePath = new File(JFinal.me().getConstants().getUploadedFileSaveDirectory()).getPath();
		if(AppHelper.isEmpty(path)){
			path = basePath;
		}
		if(path.indexOf(basePath)!=0){
			render(BjuiRender.error("文件路径不合法！", false));
			return;
		}
		//文件查看
		File d = new File(path);
		if(!d.exists()){
			render(BjuiRender.error("文件路径不存在！", false));
			return;
		}
		if(d.isDirectory()){
			File list[] = d.listFiles();
			setAttr("list", list);
			setAttr("parentPath",d.getParent());
			setAttr("thisPath","\\"+d.getPath().substring(basePath.length()));
		}else{
			renderFile(d);
			return;
		}
	}
	public void listdelete(){
		String path=getPara("path","");
		String basePath = new File(JFinal.me().getConstants().getUploadedFileSaveDirectory()).getPath();
		if(AppHelper.isEmpty(path)){
			path = basePath;
		}
		if(path.indexOf(basePath)!=0){
			render(BjuiRender.error("路径不合法！", false));
			return;
		}
		File d = new File(path);
		if(!d.exists()||d.isDirectory()){
			render(BjuiRender.error("路径不存在！", false));
			return;
		}
		if(!d.delete()){
			render(BjuiRender.error("文件删除失败！", false));
			return;
		}
		render(BjuiRender.success("删除成功！", false));
	}
}
